package com.help10000.rms.ui.activitys

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.beans.BalanceDesc
import com.amir.common.beans.WalletDescData
import com.amir.common.weight.DuplicateClickProxy
import com.amir.common.weight.SpaceItemDecoration
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.WalletDescPresenter
import com.help10000.rms.mvp.view.WalletDescView
import com.help10000.rms.ui.adapter.WalletDescAdapter
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.windows.TimePickerDialog
import kotlinx.android.synthetic.main.activity_wallet_desc.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 钱包明细
 */
class WalletDetailActivity : BaseActivity<WalletDescPresenter, WalletDescView>(), WalletDescView,
    View.OnClickListener, TimePickerDialog.TimePickerDialogInterface {

    var list = mutableListOf<BalanceDesc>()
    var PageNo = 1
    var startTime = ""
    var endTime = ""
    var type = 0//0 开始时间
    var mTimePickerDialog: TimePickerDialog? = null
    var adapter: WalletDescAdapter? = null;
    override fun getlayoutId(): Int {
        return R.layout.activity_wallet_desc
    }

    override fun initView() {
        initRefreshLayout(refresh)
        mTimePickerDialog = TimePickerDialog(this)
    }

    var fomart = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

    @RequiresApi(Build.VERSION_CODES.O)
    override fun initData(savedInstanceState: Bundle?) {
        //获取前一天的时间
        //获取前一天的时间
        val calendar: Calendar = Calendar.getInstance()
        endTime = fomart.format(calendar.time)

        calendar.add(Calendar.MONTH, -3) //每月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        startTime = fomart.format(calendar.time)

        tv_starttime.text = startTime
        tv_endtime.text = endTime

        mPresenter.getWalletDesc(startTime, endTime, PageNo)
        val linearLayoutManager = LinearLayoutManager(mContext)
        rv_list.layoutManager = linearLayoutManager
        list.clear()
        adapter = WalletDescAdapter(mContext, list, R.layout.item_wallet_desc);
        rv_list.adapter = adapter
        rv_list.addItemDecoration(SpaceItemDecoration(0, 10))
    }

    override fun initListener() {
        super.initListener()
        tv_starttime.setOnClickListener(DuplicateClickProxy(this))
        tv_endtime.setOnClickListener(DuplicateClickProxy(this))
        tv_search.setOnClickListener(DuplicateClickProxy(this))

    }

    override fun downOnRefresh() {
        PageNo = 1
        mPresenter.getWalletDesc(startTime, endTime, PageNo)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_starttime -> {
                type = 0


                mTimePickerDialog?.showDatePickerDialog()
                val date = fomart.parse(startTime)
                var calendar = Calendar.getInstance()
                calendar.time = date
                mTimePickerDialog?.updateDate(calendar)
            }
            R.id.tv_endtime -> {
                type = 1
                mTimePickerDialog?.showDatePickerDialog()
                val date = fomart.parse(endTime)
                var calendar = Calendar.getInstance()
                calendar.time = date
                mTimePickerDialog?.updateDate(calendar)
            }
            R.id.tv_search -> {
                PageNo = 1

                mPresenter.getWalletDesc(startTime, endTime, PageNo)
            }
        }
    }

    override fun loadMore() {
        PageNo++
        super.loadMore()
        mPresenter.getWalletDesc(startTime, endTime, PageNo)
    }

    override fun positiveListener() {
        if (type == 0) {
            startTime =
                "${mTimePickerDialog?.year}-${mTimePickerDialog?.month}-${mTimePickerDialog?.day}"
            tv_starttime.text = startTime
        } else {
            endTime =
                "${mTimePickerDialog?.year}-${mTimePickerDialog?.month}-${mTimePickerDialog?.day}"
            tv_endtime.text = endTime
        }


    }

    override fun negativeListener() {

    }

    override fun createPresenter(): WalletDescPresenter {
        return WalletDescPresenter(this)
    }

    override fun dataSuccess(data: WalletDescData) {
        onRefreshFinish(true)
        onLoadMoreFinish(true)
        tv_totalnum.text = data.Count.toString()
        tv_totalMoney.text = data.SumJinEr
        if (PageNo == 1) {
            if (data.BalanceList.isEmpty()) {
                tv_empty.visibility = View.VISIBLE
                onFinishNoMore()
            } else {
                tv_empty.visibility = View.GONE
            }
            list.clear()
            list.addAll(data.BalanceList)
            adapter?.notifyDataSetChanged()
        } else {
            if (data.BalanceList.isEmpty()) {
                onFinishNoMore()
                return
            }
            var oldCount = list.size
            list.addAll(data.BalanceList)
            adapter?.notifyItemRangeInserted(oldCount, data.BalanceList.size)
        }

    }
}
